package Client.Operations;

import Library.Common.GUIOperations;
import Library.Common.Message;
import Library.Common.OperationsRefactor;
import java.io.*;
import java.net.*;
import javax.swing.JTextPane;

public class ClientSession {

    public static Socket connect(String destinationHost, int port, JTextPane panel) {
        Socket connection = null;
        try {
            GUIOperations.updateTextPane(panel, "Conectando al Servidor...");
            connection = new Socket(destinationHost, port);
            if (connection.isConnected()) {
                GUIOperations.updateTextPane(panel, "Conectado al Servidor");
            }
        } catch (IOException e) {
            GUIOperations.updateTextPane(panel, "El Servidor no está listo");
            e.printStackTrace();
        }
        return connection;
    }

    public static void disconnect(Socket connection, ObjectOutputStream output, ObjectInputStream input,
            JTextPane panelClient, JTextPane panelServer) {
        try {
            OperationsRefactor.outputMessage(output, new Message("disconnect"));
            GUIOperations.updateTextPane(panelServer, input);
            GUIOperations.updateTextPane(panelClient, "Desconexion Realizada con éxito");
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
